package com.lottery.system.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author: xiaoxie
 * create: 2024-09-23 21:33
 * @BelongsProject: study-rabbitmq
 * @BelongsPackage: com.lottery.system.rabbitmq
 * description: rabbit mq 的生产者
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.建立连接
        ConnectionFactory factory = new ConnectionFactory();
        //ip
        factory.setHost("81.70.210.221");
        //端口号
        factory.setPort(5672);
        //用户名
        factory.setUsername("study");
        //密码
        factory.setPassword("010920");
        //虚拟机
        factory.setVirtualHost("study");
        //
        Connection connection = factory.newConnection();
        //2.创建信道
        Channel channel = connection.createChannel();
        //3.声明交换机,由于是入门所以就使用默认的交换机
        //4.声明队列
        /**
         * @description:
         * @author: xiaoxie
         * @date: 2024/9/23 21:46
         * @param: queue: 队列名称 durable: 是否持久化 exclusive: 是否独占(一个消费者使用)
         *         autoDelete: 是否自动删除 arguments: 参数
         * @return: void
         */
        channel.queueDeclare("test",true,false,false,null);
        //5.发送信息

        /**
         * @description:
         * @author: xiaoxie
         * @date: 2024/9/23 21:56
         * @param: exchange: 交换机(使用默认就不写) routingKey: 发送队列, props: 规则 data: 消息
         * @return: void
         **/
        for (int i = 0; i < 10; i++) {
            String message = "Hello RabbitMQ!" + i;
            channel.basicPublish("","test",null,message.getBytes());
        }

        //6.关闭资源
        System.out.println("信息发送成功");
        channel.close();
        connection.close();
    }
}
